import { Component, Input, ViewChild } from '@angular/core';
import { DataTableComponent, TableWidthConfig } from 'ng-devui';
import { filter, map } from 'rxjs';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { SalesService } from 'src/app/@core/services/sales/sales.service';

@Component({
  selector: 'app-select-sales-invoice',
  templateUrl: './select-sales-invoice.component.html',
  styleUrls: ['./select-sales-invoice.component.scss']
})
export class SelectSalesInvoiceComponent {
  @Input() data: any;
  tableheight = '230px';
  creatTime: any = []
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '单据单号',
        fieldType: 'text',
        fixedLeft: '80px'
      },
      {
        field: '客户名称',
        header: '客户名称',
        fieldType: 'text'
      },
      {
        field: '物料编码',
        header: '物料编码',
        fieldType: 'text'
      },
      {
        field: '物料名称',
        header: '物料名称',
        fieldType: 'text'
      },
      {
        field: '物料规格',
        header: '物料规格',
        fieldType: 'text'
      },
      {
        field: '物料单位',
        header: '物料单位',
        fieldType: 'text'
      },
      {
        field: '数量',
        header: '数量',
        fieldType: 'text'
      },{
        field: '可退数量',
        header: '可退数量',
        fieldType: 'text'
      },
      {
        field: '单价（含税）',
        header: '单价（含税）',
        fieldType: 'text'
      },
      {
        field: '含税价格',
        header: '含税价格',
        fieldType: 'text'
      },
      {
        field: '税额',
        header: '税额',
        fieldType: 'text'
      },
      {
        field: '折扣（%）',
        header: '折扣（%）',
        fieldType: 'text'
      },
      {
        field: '折扣单价（含税）',
        header: '折扣单价（含税）',
        fieldType: 'text'
      },
      {
        field: '折扣含税金额',
        header: '折扣含税金额',
        fieldType: 'text'
      },
      {
        field: '折扣税额',
        header: '折扣税额',
        fieldType: 'text'
      }
    ]
  };//表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field: '单据单号',
      width: '200px'
    },
    {
      field: '客户名称',
      width: '200px'
    },
    {
      field: '物料编码',
      width: '150px'
    },

    {
      field: '物料名称',
      width: '150px'
    },
    {
      field: '物料规格',
      width: '150px'
    },
    {
      field: '物料单位',
      width: '150px'
    },
    {
      field: '数量',
      width: '150px'
    },
    {
      field: '可退数量',
      width: '150px'
    },
    {
      field: '单价（含税）',
      width: '150px'
    },
    {
      field: '含税价格',
      width: '150px'
    },
    {
      field: '税额',
      width: '150px'
    },
    {
      field: '折扣（%）',
      width: '150px'
    },
    {
      field: '折扣单价（含税）',
      width: '150px'
    },
    {
      field: '折扣含税金额',
      width: '150px'
    },
    {
      field: '折扣税额',
      width: '150px'
    }
  ];//表格宽度配置
  Customer: any = []
  fifter = {
    SearchTermPart:'',
    SearchTermCustomer:'',
    SaleDeliverNum:'',
    CustomerId:'',
    State:'已发货',
    PageNumber: 1,
    PageSize: 20,
    ZhuanHuanWanDe: false,
  }
  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  tableData: any
  checkedIds: any=[]
  CheckedRows: any =[]
  filterRows: any=[]
  constructor(private SharedMethodService: SharedMethodService, private SalesService: SalesService) { }
  ngOnInit(): void {
    this.filter()
  }
  filter() {
    if(this.data.customerName){
      this.fifter.SearchTermCustomer = this.data.customerName
    }
    const filterIds = this.data.saleDeliverPartIds
    this.SalesService.GetSaleDeliverPart(this.fifter).subscribe((data: any) => {
      this.tableData = data.items.map((item:any)=>{
        if(filterIds.includes(item.saleDeliverPartId)){
          return{
            ...item,
            $checked: true,
            $halfChecked: false,
          }
        }
        return item
      })
      this.pager.total = data.totalCount;
      this.CheckedRows = this.tableData.filter((item: any) => item.$checked);
    })
  }
  filter2() {
    this.fifter.PageNumber = 1
    this.filter()
  }
  reset() {
    this.fifter = {
      SearchTermPart:'',
      ZhuanHuanWanDe: false,
    SearchTermCustomer:this.data.customerName,
    SaleDeliverNum:'',
    CustomerId:'',
    State:'已发货',
    PageNumber: 1,
    PageSize: 20
    }
    this.filter()
  }

  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(
      this.datatable,
      'saleDeliverPartId'
    ).subscribe((data: any) => {
      this.checkedIds = data;
      this.CheckedRows = this.tableData.filter((item: any) => item.$checked);
    });

  }//获取选中行
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }) {
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem, this.datatable)
  }
  getCheckedRowsData() {
    this.SharedMethodService.getCheckedRowsData(this.datatable).subscribe(
      (data: any) => {
          const filterIds = this.data.saleDeliverPartIds; // 确保这是一个数组
          this.CheckedRows = data;
          this.tableData = this.tableData.map((item: any) => {
            if (filterIds.includes(item.saleDeliverPartId)) {
              return {
                ...item,
                $checked: true,
                $halfChecked: false
              };
            }
            return item;
          });
          this.CheckedRows = this.tableData.filter((item: any) => item.$checked);
          this.filterRows = this.CheckedRows.filter((item: any) => !filterIds.includes(item.saleDeliverPartId));
      }
    );
  }
  pageIndexChange($event: any) {
    this.filter()
  }//切换页码
  pageSizeChange($event: any) {
    this.filter()
  }//改变每页显示数量
  cleared(){
    this.reset()
  }
}
